package com.digital.smartgrow.service.sysinfo.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.dao.sysinfo.DictionaryItemMapper;
import com.digital.smartgrow.entity.sysinfo.Dictionary;
import com.digital.smartgrow.entity.sysinfo.DictionaryItem;
import com.digital.smartgrow.service.sysinfo.DictionaryItemService;
import com.digital.smartgrow.service.sysinfo.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典项 服务实现类
 * </p>
 *
 * @author xiewb
 * @since 2022-04-23
 */
@Service
public class DictionaryItemServiceImpl extends ServiceImpl<DictionaryItemMapper, DictionaryItem> implements DictionaryItemService {

    @Autowired
    private DictionaryService dictionaryService;

//    @Override
//    public Object getListByPage(DictionaryItemPageDTO pageDTO) {
//        Page<DictionaryItem> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
//        QueryWrapper<DictionaryItem> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getEmployeeId()), "employeeId", pageDTO.getEmployeeId());
//        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getName()), "name", pageDTO.getName());
//        return this.page(page, queryWrapper);
//    }

    @Override
    public Object getInfo(Integer id) {
        return this.getById(id);
    }

    @Override
    public Object getList(String dictNumber) {
        List<Dictionary> dictionaryList = dictionaryService.lambdaQuery()
                .eq(Dictionary::getStatus, 1)
                .eq(Dictionary::getDeleted, 0)
                .list();
        List<DictionaryItem> dictionaryItemList = this.lambdaQuery()
                .eq(DictionaryItem::getStatus, 1)
                .eq(DictionaryItem::getDeleted, 0)
                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                .list();
        Map<String, List<DictionaryItem>> dictItemMap = dictionaryItemList.stream().collect(Collectors.groupingBy(DictionaryItem::getDictNumber));
        List<Map> list = new ArrayList<>();
        for (Dictionary dict : dictionaryList) {
            Map map = new HashMap();
            map.put("number", dict.getDictNumber());
            map.put("name", dict.getDictName());
            List<Map> itemList = new ArrayList<>();
            for (DictionaryItem dictItem : dictItemMap.getOrDefault(dict.getDictNumber(), new ArrayList<>())) {
                Map item = new HashMap();
                item.put("itemNumber", dictItem.getItemNumber());
                item.put("itemName", dictItem.getItemName());
                itemList.add(item);
            }
            map.put("list", itemList);
            list.add(map);
        }
        return list;
    }
}
